package com.atguigu.cloud.config.securityConfig.Request;/**
 * @Auter 李孝在
 * @Date 2024/11/18
 */

import com.alibaba.fastjson2.JSON;
import com.atguigu.cloud.tool.resp.ResultData;
import com.atguigu.cloud.tool.resp.ReturnCodeEnum;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import java.io.IOException;

/**
 * @projectName: keshe
 * @package: com.atguigu.cloud.config.securityConfig.Request
 * @className: asd
 * @author: Eric
 * @description: TODO
 * @date: 2024/11/18 11:43
 * @version: 1.0
 */

public class BaseAuthenticationFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        //获取错误信息
        String localizedMessage = exception.getLocalizedMessage();

        ResultData<Object> result=ResultData.fail(ReturnCodeEnum.RC204.toString(),localizedMessage);

        //转换成json字符串
        String json = JSON.toJSONString(result);
        //返回响应
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().println(json);
    }
}
